找传奇、传世资源到传世资源站!

服务器作为中介,实现客户端之间的数据通信​

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

服务器作为中介,实现客户端之间的数据通信服务器作为中介,实现客户端之间的数据通信​ C#语言基础-第1张   //监听客户端发来的请求 private void watchconnecting() { Socket connection = null; while (true) //持续不断监听客户端发来的请求 { try { connection = socketwatch.Accept(); } catch (Exception ex) { rec_info.AppendText(ex.Message); //提示套接字监听异常 break; } //获取客户端的IP和端口号 IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address; int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port; //让客户显示"连接成功的"的信息 string sendmsg = "连接服务端成功!\r\n" "本地IP:" clientIP ",本地端口" clientPort.ToString(); // byte[] arrSendMsg = Encoding.ASCII.GetBytes(sendmsg); byte[] arrSendMsg = Encoding.GetEncoding("GB2312").GetBytes(sendmsg); connection.Send(arrSendMsg); RemoteEndPoint = connection.RemoteEndPoint.ToString(); //客户端网络结点号 rec_info.AppendText("成功与" RemoteEndPoint "客户端建立连接!\t\n"); //显示与客户端连接情况 dic.Add(RemoteEndPoint, connection); //添加客户端信息 // dic[RemoteEndPoint].Send(Encoding.ASCII.GetBytes("Server Say Hello")); //发送数据 OnlineList_Disp(RemoteEndPoint); //显示在线客户端 //IPEndPoint netpoint = new IPEndPoint(clientIP,clientPort); IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint; //创建一个通信线程 ParameterizedThreadStart pts = new ParameterizedThreadStart(recv); Thread thread = new Thread(pts); thread.IsBackground = true;//设置为后台线程,随着主线程退出而退出 //启动线程 thread.Start(connection); } }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复